class_name GamingView
extends BaseView

#region 属性
## 进度条动画时间
@export_group("Health")
@export var attenuation_time:float = 0.5
@export var health_pb_back:TextureProgressBar
@export var health_pb_fore:TextureProgressBar
@export_group("")
@export_group("Stamina")
@export var stamina_pb:ProgressBar
@export_group("")
@export_group("Weapon")
@export var weapon_capacity_lab:RichTextLabel
@export var weapon_icon:TextureRect
@export_group("")
var entity:Entity
var max_health:float = 0
var max_stamina:float = 0
var health_tween:Tween
var connect_obj_dict:Dictionary[Signal,ConnectObj] = {}
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
func init_view():
	super.init_view()
func on_enter(args:Dictionary = {}):
	super.on_enter(args)
	var player = GamePlayerSystem.get_player()
	if player:
		entity = Utils.find_child_by_type(player,Entity,-1)
		if entity and entity.entity_data:
			max_health = entity.entity_data.get_attribute_value("MaxHealth")
			health_pb_fore.value = 0
			_recover(entity.entity_data.get_attribute_value("Health"))
			connect_obj_dict[entity.entity_data.data_changed] = ConnectObj.new(entity.entity_data.data_changed,[_on_entity_data_changed])
			var weapon_component:WeaponComponent = entity.get_component(WeaponComponent)
			if weapon_component:
				_on_entity_weapon_changed(null,weapon_component.cur_weapon)
				_on_weapon_data_changed("attribute","Capacity",weapon_component.cur_weapon.get_weapon_data().get_attribute_value("Capacity"),weapon_component.cur_weapon.get_weapon_data().get_attribute_value("Capacity"))
				connect_obj_dict[weapon_component.cur_weapon_changed] = ConnectObj.new(weapon_component.cur_weapon_changed,[_on_entity_weapon_changed])
			_on_stamina_changed()
func on_pause(args:Dictionary = {}):
	super.on_pause(args)
func on_resume(args:Dictionary = {}):
	super.on_resume(args)
func on_exit(args:Dictionary = {}):
	for connect_obj:ConnectObj in connect_obj_dict.values():
		if connect_obj:
			connect_obj.disconnect_all()
	entity = null
	super.on_exit(args)
func get_view_state() -> GameEnum.ViewState:
	return super.get_view_state()
#endregion
#region 公共方法
#endregion
#region 私有方法
func _hit(new_value):
	if health_tween and health_tween.is_valid():
		health_tween.kill()
	health_tween = create_tween()
	var fill_value = float(new_value) / float(max_health)
	health_pb_fore.value = fill_value
	health_tween.tween_property(self.health_pb_back,"value",fill_value,attenuation_time).set_ease(Tween.EASE_OUT)
func _recover(new_value):
	if health_tween and health_tween.is_valid():
		health_tween.kill()
	health_tween = create_tween()
	var fill_value = float(new_value) / float(max_health)
	health_pb_back.value = fill_value
	health_tween.tween_property(self.health_pb_fore,"value",fill_value,attenuation_time).set_ease(Tween.EASE_OUT)
func _max_health_changed(new_max_health):
	if health_tween and health_tween.is_valid():
		health_tween.kill()
	health_tween = create_tween()
	var cur_health = entity.entity_data.get_attribute_value("Health")
	var fill_value = float(cur_health) / float(max_health)
	if float(cur_health) / float(max_health + 1) >= float(cur_health) / float(new_max_health + 1):
		health_pb_fore.value = fill_value
		health_tween.tween_property(self.health_pb_back,"value",fill_value,attenuation_time).set_ease(Tween.EASE_OUT)
	else:
		health_pb_back.value = fill_value
		health_tween.tween_property(self.health_pb_fore,"value",fill_value,attenuation_time).set_ease(Tween.EASE_OUT)
	max_health = new_max_health
func _on_entity_data_changed(tag_space:String,tag:String,old_value,new_value):
	if tag_space == "attribute":
		if tag == "Health":
			if new_value > old_value:
				_recover(new_value)
			elif new_value < old_value:
				_hit(new_value)
		elif tag == "MaxHealth":
			_max_health_changed(new_value)
		elif tag == "Stamina" or tag == "MaxStamina":
			_on_stamina_changed()
func _on_entity_weapon_changed(last_weapon:Weapon,cur_weapon:Weapon):
	if last_weapon:
		if connect_obj_dict.has(last_weapon.entity.entity_data.data_changed):
			var connect_obj:ConnectObj = connect_obj_dict.get(last_weapon.entity.entity_data.data_changed) as ConnectObj
			connect_obj.disconnect_all()
			connect_obj_dict.erase(last_weapon.entity.entity_data.data_changed)
	if cur_weapon:
		weapon_icon.texture = cur_weapon.get_weapon_data().sprite
		connect_obj_dict[cur_weapon.get_weapon_data().data_changed] = ConnectObj.new(cur_weapon.get_weapon_data().data_changed,[_on_weapon_data_changed])
	else:
		weapon_capacity_lab.visible = false
		weapon_icon.texture = null
func _on_weapon_data_changed(tag_space:String,tag:String,old_value,new_value):
	if tag_space == "attribute" and tag == "Capacity":
		var weapon_component:WeaponComponent = entity.get_component(WeaponComponent)
		if weapon_component:
			var total_count = weapon_component.get_cur_weapon().entity.entity_data.get_attribute_value("MaxCapacity")
			if new_value < 0:
				weapon_capacity_lab.visible = true
				weapon_capacity_lab.text = "∞"
			elif total_count < 0:
				weapon_capacity_lab.visible = true
				weapon_capacity_lab.text = "%d/∞" % new_value
			else:
				weapon_capacity_lab.visible = true
				weapon_capacity_lab.text = "%d/%d" %[new_value,total_count]
func _on_stamina_changed():
	stamina_pb.value = entity.entity_data.get_attribute_value("Stamina",0) * 1.0 / entity.entity_data.get_attribute_value("MaxStamina",0.001)
#endregion
#region 生命周期函数
#endregion
